package com.p595263720.o_amap;
import android.app.Activity;
import android.content.Context;

import androidx.annotation.NonNull;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleOwner;
import com.p595263720.o_amap.location.LocationHandlers;
import com.p595263720.o_amap.map.MapViewFactory;
import com.p595263720.o_amap.nav.NavHandlers;
import com.p595263720.o_amap.search.SearchHandlers;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.embedding.engine.plugins.lifecycle.FlutterLifecycleAdapter;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.PluginRegistry;

/**
 * OAmapPlugin
 */
public class OAmapPlugin implements FlutterPlugin,ActivityAware {
    private static MethodChannel channel_search;
    private static MethodChannel channel_loction;
    private static MethodChannel channel_nav;

    private FlutterPluginBinding pluginBinding;
    private Lifecycle lifecycle;
    private static final String VIEW_TYPE = "com.p595263720.zit/GD";

    @Override
    public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
        pluginBinding = flutterPluginBinding;
        flutterPluginBinding.getPlatformViewRegistry().registerViewFactory("com.p595263720.zit/GD",
                new MapViewFactory(flutterPluginBinding.getBinaryMessenger(), new LifecycleProvider() {
                    @Override
                    public Lifecycle getLifecycle() {
                        return lifecycle;
                    }
                }
                ));
        setHandler(flutterPluginBinding.getFlutterEngine().getDartExecutor(),flutterPluginBinding.getApplicationContext());
    }

    public void registerWith(PluginRegistry.Registrar registrar) {
        final Activity activity = registrar.activity();
        if (activity == null) {
            return;
        }
        if(activity instanceof LifecycleOwner){
            registrar.platformViewRegistry().registerViewFactory(VIEW_TYPE,new MapViewFactory(registrar.messenger(), new LifecycleProvider() {
                @Override
                public Lifecycle getLifecycle() {
                    return ((LifecycleOwner) activity).getLifecycle();
                }
            }));
        }else{
            registrar
                    .platformViewRegistry()
                    .registerViewFactory(
                            VIEW_TYPE,
                            new MapViewFactory(registrar.messenger(), new ProxyLifecycleProvider(activity)));
        }
        setHandler(registrar.messenger(),registrar.context());
    }


    public static void setHandler(BinaryMessenger messenger, Context context) {
        //注册搜索
        channel_search = new MethodChannel(messenger, "com.p595263720.zit/search");
        channel_search.setMethodCallHandler(new SearchHandlers(context));
        
        //注册定位
        channel_loction = new MethodChannel(messenger, "com.p595263720.zit/location");
        channel_loction.setMethodCallHandler(new LocationHandlers(messenger,context));

        //注册导航
        channel_nav = new MethodChannel(messenger, "com.p595263720.zit/nav");
        channel_nav.setMethodCallHandler(new NavHandlers(context));
    }


    @Override
    public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
        channel_search.setMethodCallHandler(null);
        channel_loction.setMethodCallHandler(null);
        channel_nav.setMethodCallHandler(null);
        pluginBinding = null;
    }

    @Override
    public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) {
        lifecycle = FlutterLifecycleAdapter.getActivityLifecycle(binding);
    }

    @Override
    public void onDetachedFromActivity() {
        lifecycle = null;
    }

    @Override
    public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) {
        onAttachedToActivity(binding);
    }

    @Override
    public void onDetachedFromActivityForConfigChanges() {
        this.onDetachedFromActivity();
    }
}
